const fs = require('fs');
const path = require('path');
const table = require('text-table');
const { green, magenta, red } = require('chalk');
const { resolve, highlightTheme } = require('hexo-theme-next/scripts/events/lib/utils');

function test(color) {
  const reg = /^\s*(#([\da-fA-F]{6}|[\da-fA-F]{3})|black|white)(;|)\s*$/;
  return reg.test(color.replace(/\/\*.*?\*\//g, ''));
}

const basename = resolve('highlight.js', 'styles/');
const list = fs.readdirSync(basename)
  .filter(name => name.endsWith('.css'))
  .map(name => highlightTheme(name.replace('.css', '')))
  .filter(({ background, foreground }) => {
    return !(test(background) && test(foreground));
  });

console.log(table(list.map(({ file, background, foreground }) => {
  return [green(file.replace(basename, '')), magenta(background.trim()), red(foreground.trim())];
})));

fs.writeFileSync(path.join(__dirname, '../source/highlight/unavailable.js'), `// This file is automatically generated using \`npm run highlight\`
const unavailable = ${JSON.stringify(list.map(({ file }) => file.replace(basename, '').replace('.css', '')))};
`);
